//
//  MCTabBarController.swift
//  WisdomSpace
//
//  Created by goulela on 2017/8/29.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit

class MCTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        addAllChildViewController()
        
        self.selectedIndex = 0
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    
    // MARK: - Method
    private  func baseSetting() {
        let item = UITabBarItem.appearance()
        
        let normal = NSMutableDictionary.init()
        normal[NSAttributedStringKey.foregroundColor] = UIColor.MCTabbarNormal
        
        let selected = NSMutableDictionary.init()
        selected[NSAttributedStringKey.foregroundColor] = UIColor.MCMain
        
        item.setTitleTextAttributes(normal as? [NSAttributedStringKey : Any], for: UIControlState.normal)
        item.setTitleTextAttributes(selected as? [NSAttributedStringKey : Any], for: UIControlState.selected)
        
        self.tabBar.backgroundImage = UIImage.init(color: UIColor.white)
    }
    
    
    func addAllChildViewController() {
        
        let one = MCHomePageViewController()
        let oneNav = MCNavigationController.init(rootViewController: one)
        setUpOneChildViewController(nav: oneNav, image: "Tabbar_one", seletedImage: "Tabbar_one_sel", title: "商城")
        
        let two = MCActivityPageViewController()
        let twoNav = MCNavigationController.init(rootViewController: two)
        setUpOneChildViewController(nav: twoNav, image: "Tabbar_two", seletedImage: "Tabbar_two_sel", title: "营销活动")
        
        
        let three = MCPersonalViewController()
        let threeNav = MCNavigationController.init(rootViewController: three)
        setUpOneChildViewController(nav: threeNav, image: "Tabbar_three", seletedImage: "Tabbar_three_sel", title: "我的")
    }

    
    private func setUpOneChildViewController(nav : UINavigationController, image : NSString, seletedImage : NSString, title : NSString) {
        nav.view.backgroundColor = UIColor.white
        nav.tabBarItem.title = title as String
        nav.tabBarItem.image = imageWithOriginal(imageName: image)
        nav.tabBarItem.selectedImage = imageWithOriginal(imageName: seletedImage)
        addChildViewController(nav)
    }
    
    private func imageWithOriginal(imageName : NSString) -> UIImage {
        let image : UIImage = UIImage.init(named: imageName as String) ?? UIImage()
        
        return image.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
    }
    
    // MARK: - Setter & Getter

}
